package thread.atomic;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author lwmzzz
 * @version 1.0
 * @date 2021/4/26 23:51
 * @description
 */
public class AtomicTest {
    public static void main(String[] args) {
        MyInt myInt = new MyInt();
        new Thread(() -> {
            while (true) {
                System.out.println(Thread.currentThread().getName() + "=>" + myInt.getNum());
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        new Thread(() -> {
            while (true) {
                System.out.println(Thread.currentThread().getName() + "=>" + myInt.getNum());
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    static class MyInt {
        int num;

        public int getNum() {
            return num++;
        }
    }

    //原子操作类
    static class MyAtomicInt {
        AtomicInteger atomicInteger = new AtomicInteger();

        public int getNum() {
            return atomicInteger.getAndIncrement();
        }
    }
}
